import { ReflectUtil } from "../di"
import { getBeanId } from "../common"

export function Inject(id: string = null) {
  return function (target, targetKey: string) {
    /**
     * 将一个 clazz 上所有的 @Inject 合并
     * @Inject() aa:A
     * @Inject() bb:B
     * props = {
     *   'a' : {clazz: A, id:'a', prop: 'aa'},
     *   'b' : {clazz: B, id:'b', prop: 'bb'}
     * }
     */
    const clazz = ReflectUtil.getType(target, targetKey) || {}
    if (id == null) id = getBeanId(clazz)
    const props = {
      ...ReflectUtil.getInjects(target),
      [targetKey]: {
        clazz: clazz,
        id: id,
        prop: targetKey
      }
    }
    ReflectUtil.defineInject(target, props)
  }
}
